package com.cloudptt.api.product.func.call;

import java.util.List;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.product.ISuperCallOperator;
import com.cloudptt.api.product.config.call.AnswerModeConfig;
import com.cloudptt.api.product.func.IFunc;
import com.cloudptt.api.product.func.addr.IAddrOp;
import com.cloudptt.api.product.func.call.group.GroupCallRecvListener;
import com.cloudptt.api.product.func.call.group.GroupCallTransListener;
import com.cloudptt.api.product.func.call.group.HalfCallInfo;
import com.cloudptt.api.product.func.call.group.GroupCallSpeakListener;
import com.cloudptt.api.product.func.call.group.GroupCallTracker;
import com.cloudptt.api.product.func.call.group.fsm.GroupCallConferenceListener;
import com.cloudptt.api.product.func.call.single.CallInfo;
import com.cloudptt.api.product.func.call.single.SingleCallTracker;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.CallOperator;
import com.mcptt.config.model.McIdInfo;
import com.mcptt.config.model.BootStrapSvrInfo;

import android.content.Context;



public class CallTracker implements IFunc,ISuperCallOperator,ICallMute
{
	private final static String THIS_FILE = "CallTracker";
	private GroupCallTracker groupCallTracker = null;
	private SingleCallTracker singleCallTracker = null;
	private CallOperator operator = null;

	public CallTracker(Context ctx, McIdInfo mcIdInfo, IAddrOp addOp, AnswerModeConfig answerModeConfig,CallOperator operator)
	{
		this.groupCallTracker = new GroupCallTracker(ctx,mcIdInfo,addOp,answerModeConfig,operator,this);
		this.singleCallTracker = new SingleCallTracker(ctx,mcIdInfo,addOp,answerModeConfig,operator,this);
		this.operator = operator;
	}

	public int newCall(CallAttr callAttr)
	{
		Log.debug(this.getClass(), "newCall  callAttr = " + callAttr);
		if(operator != null)
		{
			int rusult = operator.checkOperate();
			if(CauseCont.OperateCheck_Suc != rusult)
			{
				return rusult;
			}
		}

		int callType = callAttr.getCallType();
		if(CallAttr.isGroupCall(callType))
		{
			return groupCallTracker.newCall(callAttr);
		}
		else if(CallAttr.isPrivateCall(callType)
				|| CallAttr.isAmbient(callType))
		{
			return singleCallTracker.newCall(callAttr);
		}
		return CauseCont.OperateCheck_Call_CallTypeErr;
	}

	public int doUserAction(String callKey,int userAction,String jsonStr)
	{
		Log.debug(this.getClass(), "doUserAction callKey = " + callKey + " userAction = " + CallConst.userActionTag(userAction) + " jsonStr = " + jsonStr);
		boolean isHasCall = groupCallTracker.isHasCall(callKey);
		Log.debug(this.getClass(), "doUserAction groupCallTracker isHasCall = " + isHasCall);
		if(isHasCall
				&& userAction != CallConst.BtnAction_Make_Video)
		{
			return groupCallTracker.doUserAction(callKey,userAction,jsonStr);
		}
		else
		{
			isHasCall = singleCallTracker.isHasCall(callKey);
			Log.debug(this.getClass(), "doUserAction pCallTracker isHasCall = " + isHasCall);
			if(isHasCall)
			{
				return singleCallTracker.doUserAction(callKey, userAction);
			}
		}
		return CauseCont.OperateErr_NoAgent;
	}

	public int doRecvAction(String callKey,int btnAction,String userId)
	{
		return CauseCont.OperateCheck_Suc;
    }
	
	public boolean isHasCall(String callKey)
	{
		boolean isHasCall = groupCallTracker.isHasCall(callKey);
		if(!isHasCall)
		{
			isHasCall = singleCallTracker.isHasCall(callKey);
			Log.debug(this.getClass(), "isHasCall isHasCall = " + isHasCall);
			return isHasCall;
//			return isHasCall;
		}
		return isHasCall;
	}

	public boolean setSpeakerphoneEnable(String callKey, boolean enable)
	{
		boolean isHasCall = groupCallTracker.isHasCall(callKey);
		if(isHasCall)
		{
			return groupCallTracker.setSpeakerphoneEnable(callKey,enable);
		}
		else
		{
			isHasCall = singleCallTracker.isHasCall(callKey);
			if(isHasCall)
			{
				return singleCallTracker.setSpeakerphoneEnable(callKey,enable);
			}
		}
		return true;
	}

	public boolean setMicrophoneEnable(String callKey, boolean isOn)
	{
		boolean isHasCall = groupCallTracker.isHasCall(callKey);
		if(isHasCall)
		{
			return groupCallTracker.setMicrophoneEnable(callKey,isOn);
//			return isOn;
		}
		else
		{
			isHasCall = singleCallTracker.isHasCall(callKey);
			if(isHasCall)
			{
				return singleCallTracker.setMicrophoneEnable(callKey,isOn);
			}
		}
		return true;
	}
	
	public boolean isHaveActivePCall()
	{
		return singleCallTracker.isHaveActivePCall();
	}

//	public boolean isPCallMicMute(String callKey)
//	{
//		return singleCallTracker.isMicMute(callKey);
//	}
	public HalfCallInfo getHalfCallInfo(String callKey)
	{
		HalfCallInfo info = groupCallTracker.getCallInfo(callKey);
		return info;
	}
	
	public List<HalfCallInfo> getGroupCallInfoList()
	{
		Log.d(THIS_FILE,"getGroupCallInfoList" );
		List<HalfCallInfo> list = groupCallTracker.getTalkInfoList();
		return list;
	}

	public List<CallInfo> getSingleCallInfoList()
	{
		Log.d(THIS_FILE,"getGroupCallInfoList" );
		List<CallInfo> list = singleCallTracker.getCallInfoList();
		return list;
	}
	
	public String getCallDesc()
	{
		return groupCallTracker.getCallDesc() + singleCallTracker.getCallDesc();
	}
	@Override
	public boolean isActiveCall(String callKey) {
		// TODO Auto-generated method stub
		boolean isActiveCall = groupCallTracker.isActiveCall(callKey);
		if(!isActiveCall)
		{
			isActiveCall = singleCallTracker.isActiveCall(callKey);
			return isActiveCall;
		}
		return isActiveCall;
	}


	public void setPCallClass(Class<?> activityCls,Class<?> floatWindowServiceCls)
	{
		singleCallTracker.setPrivacyClass(activityCls,floatWindowServiceCls);
	}
	public void setIncommingCall(IIncommingCall incommingCall) {
		singleCallTracker.setIncommingCall(incommingCall);
		groupCallTracker.setIncommingCall(incommingCall);
	}

	public void setGCallVideoClass(Class cls)
	{
		groupCallTracker.setGCallVideoClass(cls);
	}
	
	public void setSingleCallListener(CallListener callListener)
	{
		Log.debug(this.getClass(), "McpttCallTracker setPCallListener");
		singleCallTracker.setListener(callListener);
	}
	public void setShowFloatWin(String callKey, boolean isLaucher)
	{
		Log.debug(this.getClass(), "McpttCallTracker setSingleCallLaucher");
		singleCallTracker.setShowFloatWin(callKey,isLaucher);
	}
	public void setGCallListener(CallListener callListener,GroupCallSpeakListener speakListener)
	{
		Log.debug(this.getClass(), "McpttCallTracker setGCallListener");
		groupCallTracker.setListener(callListener,speakListener);
	}
	public void setGCallVideoListener(CallListener callListener, GroupCallTransListener transListener, GroupCallRecvListener recvListener)
	{
		Log.debug(this.getClass(), "McpttCallTracker setGCallVideoListener");
		groupCallTracker.setVideoListener(callListener,transListener,recvListener);
	}
	public void setGCallConferenceListener(GroupCallConferenceListener listener)
	{
		Log.debug(this.getClass(), "McpttCallTracker setGCallConferenceListener");
		groupCallTracker.setGCallConferenceListener(listener);
	}

	@Override
	public void onLanChange(String lanType) {
		// TODO Auto-generated method stub

	}
	@Override
	public void onLogined(McIdInfo mcIdInfo, BootStrapSvrInfo info, boolean isLoginSuc)
	{
		Log.debug(this.getClass(), "onLogined isLoginSuc = " + isLoginSuc);
	}
	@Override
	public void onPublishResult(int publishResult)
	{}
	@Override
	public void onSubscribeCmsGmsResult(boolean isSuc,int mcType)
	{}
	@Override
	public void doNetworkChange(boolean isConn)
	{
		Log.debug(this.getClass(), "doNetworkChange isConn = " + isConn);
//		groupCallTracker.doNetworkChange(isConn);
//		singleCallTracker.doNetworkChange(isConn);
	}
	@Override
	public void doForceDetach()
	{
		Log.debug(this.getClass(), "doForceDetach");
		groupCallTracker.doForceDetach();
		singleCallTracker.doForceDetach();
	}
	@Override
	public void doLogout()
	{
		Log.debug(this.getClass(), "doLogout");
		groupCallTracker.doLogout();
		singleCallTracker.doLogout();
	}
	@Override
	public void doExit()
	{
		Log.debug(this.getClass(), "CallAgent exit");
		groupCallTracker.doExit();
		singleCallTracker.doExit();
	}
	@Override
	public void onKeepAlive(boolean isconnected) {
		// TODO Auto-generated method stub
		groupCallTracker.onKeepAlive(isconnected);
		singleCallTracker.onKeepAlive(isconnected);
	}
	
	public void startSoundPlayer(int type)
	{
		groupCallTracker.startSoundPlayer(type);
	}
	
	public void setCallHistory(ICallHistory callHistory) {
		groupCallTracker.setCallHistory(callHistory);
		singleCallTracker.setCallHistory(callHistory);
	}

	public String toString() 
	{
		StringBuffer strBuff = new StringBuffer("McpttCall:");
		//		strBuff.append("callAttr = ").append(callAttr).append(",");
		//		strBuff.append("callAgent = ").append(callAgent).append(";");

		return strBuff.toString();
	}

	@Override
	public CallOperator getCallOperator() {
		// TODO Auto-generated method stub
		return operator;
	}

	@Override
	public void muteCall(boolean isOn) {
		// TODO Auto-generated method stub
		boolean isHave = singleCallTracker.isHaveActivePVoiceCall();
		Log.debug(this.getClass(), "muteCall isHave = " + isHave + " isOn = " + isOn);
		if(isHave && isOn)
		{
//			groupCallTracker.muteCall(true);
		}
		if(!isOn)
		{
//			groupCallTracker.muteCall(false);
		}
	}

	@Override
	public void screenAngle(String callKey, int angle) {
		// TODO Auto-generated method stub
		singleCallTracker.screenAngle(callKey,angle);
	}
	@Override
	public void subcribeConference(String callKey) {
		// TODO Auto-generated method stub
		groupCallTracker.subcribeConference(callKey);
	}

//	@Override
//	public void setNoSnd() {
//		// TODO Auto-generated method stub
//		Log.debug(this.getClass(), "setNoSnd");
//		if(groupCallTracker != null)
//		{
//			groupCallTracker.setNoSnd();
//		}
//	}
//	@Override
//	public void setSnd(boolean isMust)
//	{
//		// TODO Auto-generated method stub
//		Log.debug(this.getClass(), "setNoSnd");
//		if(groupCallTracker != null)
//		{
//			groupCallTracker.setSnd(isMust);
//		}
//	}
}
